<template>
    <div class="ruijie-login">
        <el-card class="box-card">
            <div slot="header" style="text-align: center;font-size: 16px">
                <span>登录</span>
            </div>
            <el-form class="ruijie-el-from" ref="form" :rules="rules" :model="form" label-width="80px" @keydown.native.enter.prevent ="login">
                <el-form-item label="用户名" prop="name">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="form.password" type="password"></el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="code">
                    <el-input id="codeinput" v-model="form.code" style="width: 185px"></el-input>
                    <span id="code" class="mycode" ></span>
                </el-form-item>
               <div class="login-btns">
                   <el-button :loading="loading" type="primary" @click="login">登录</el-button>
               </div>

            </el-form>
        </el-card>
    </div>
</template>
<script type="es6">
import API from '../API';
import KinerCode from '../libs/KinerCode';
export default {
  data () {
    return {
      form: {
        name: '',
        password: 'P@ssword',
        code: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        code: [
          { required: false, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      loading: false,
      codeImg: null
    };
  },
  methods: {
    login () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let inputcode = this.form.code.toLowerCase();
          let realcode = this.codeImg.myCode.strCode.toLowerCase();

          if (inputcode === realcode || inputcode === '') {
            this.loading = true;
            API.userAdmin({
              userId: this.form.name,
              password: this.form.password
            }).then((res) => {
              let json = res.data;

              this.loading = false;
              if (json.status === '200') {
                window.localStorage.setItem('TokenID', json.data.TokenID);
                window.localStorage.setItem('loginType', 'back');
                this.$store.commit('setUserAndRole', json.data);
                this.$notify({
                  title: '成功',
                  message: '登陆成功',
                  type: 'success'
                });
                this.$router.push({path: '/'});
              } else {
                this.$notify({
                  title: '失败',
                  message: '登陆失败',
                  type: 'error'
                });
              }
            }).catch(() => {
              this.loading = false;
            });
          } else {
            this.$notify({
              title: '验证码错误',
              type: 'error'
            });
          }
        } else {
          return false;
        }
      });
    }
  },
  mounted () {
    this.codeImg = new KinerCode({
      len: 4,
      bgColor: '#ffffff',
      codeArea: document.getElementById('code'),
      inputArea: document.getElementById('codeinput')
    });
  }
};
</script>
<style lang="less">
    @import "../styles/common.less";
    .@{prefix}{
        &login{
            width: 100%;
            height: 100%;
            background-image: url('../assets/images/login_bg.jpg');
            background-size: cover;
            background-position: center;
            position: absolute;
            .box-card{
                position: absolute;
                top: 45px;
                left: 50%;
                margin-left: -200px;
                width: 400px;
            }
            .login-btns{
                text-align: center;
            }
        }
    }
     .mycode {
         display: inline-block;
         width: 80px;
         height: 40px;
         vertical-align: middle;
         border: solid #999999 1px;
         border-radius: 10px;
         box-shadow: #000000 inset 0px 0px 2px;
     }
</style>
